/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package com.kylingroup.J2SE.Thread;

/**
 *
 * @author kylin
 */
public class TestThreadSynchronized1 implements Runnable{
    Timer timer=new Timer();
    public static void main(String args[]){
        TestThreadSynchronized1 test=new TestThreadSynchronized1();
        Thread t1=new Thread(test);
        Thread t2=new Thread(test);
        t1.setName("t1");
        t2.setName("t2");
        t1.start();
        t2.start();
    }
    public void run(){
        timer.add(Thread.currentThread().getName());
    }
}  

class Timer{
    private static int num=0;
    /*
    public void add(String name){
        synchronized(this){
            num++;
            try{
                Thread.sleep(1);
            }catch(InterruptedException e){}
            System.out.println(name+": you are the"+num);
        }
    }
    */
    public synchronized void add(String name){
        num++;
        try{
            Thread.sleep(1);
        }catch(InterruptedException e){}
        System.out.println(name+": you are the"+num);
       
    }
}